// pages/my/districtapprove/districtapprove.js
Page({
  data: {
    communityAuth: "",
    realName: "",
    mobile: "",
    idCard: "",
    village: "",
    frontPic: [],
    cardTopImg: "",
    reversePic: [],
    cardImg: ""
  },
  onLoad: function (options) {
    this.setData({
      communityAuth: options.communityAuth
    });
    if (options.communityAuth == 1) {
      getApp().globalData.http.communityCertInfo().then(res => {
        this.setData({
          realName: res.data.realName,
          mobile: res.data.mobile,
          idCard: res.data.idCard,
          village: JSON.parse(res.data.community),
          frontPic: [{
            url: res.data.cardTopImgUrl
          }],
          cardTopImg: res.data.cardTopImg,
          reversePic: [{
            url: res.data.cardImgUrl
          }],
          cardImg: res.data.cardImg,
        });
      });
    }
  },
  onShow: function () {
    let community = wx.getStorageSync("set_community");
    if (!!community) {
      this.setData({
        village: community
      });
    }
  },
  clear(e) {
    this.setData({
      [e.currentTarget.dataset.key]: ""
    });
  },
  selVillage() {
    wx.navigateTo({
      url: "/pages/location/location",
    })
  },
  uploadFront(e) {
    let filePath = e.detail.file.url;
    this.setData({
      frontPic: [{
        url: filePath,
        status: "uploading",
        message: "上传中"
      }]
    });
    getApp().globalData.http.upload("name", filePath, {
      srcType: 1
    }).then(res => {
      let tmp = this.data.frontPic;
      tmp[0].status = "success";
      this.setData({
        frontPic: tmp,
        cardTopImg: res.data.id
      });
    });
  },
  uploadReverse(e) {
    let filePath = e.detail.file.url;
    this.setData({
      reversePic: [{
        url: filePath,
        status: "uploading",
        message: "上传中"
      }]
    });
    getApp().globalData.http.upload("name", filePath, {
      srcType: 1
    }).then(res => {
      let tmp = this.data.reversePic;
      tmp[0].status = "success";
      this.setData({
        reversePic: tmp,
        cardImg: res.data.id
      });
    });
  },
  deleteFront() {
    this.setData({
      frontPic: [],
    });
  },
  deleteReverse() {
    this.setData({
      reversePic: [],
    });
  },
  getUserCommunity() {},
  submit() {
    let params = {
      realName: this.data.realName,
      mobile: this.data.mobile,
      idCard: this.data.idCard,
      community: !this.data.village ? "" : JSON.stringify(this.data.village),
      cardTopImg: this.data.cardTopImg,
      cardImg: this.data.cardImg,
    };
    if (!params.realName) {
      getApp().globalData.vant.toast("请输入真实姓名");
    } else if (!getApp().globalData.reg.realName.test(params.realName)) {
      getApp().globalData.vant.toast("请输入正确的真实姓名");
    } else if (!params.mobile) {
      getApp().globalData.vant.toast("请输入手机号");
    } else if (!getApp().globalData.reg.tel.test(params.mobile)) {
      getApp().globalData.vant.toast("请输入正确的手机号");
    } else if (!params.idCard) {
      getApp().globalData.vant.toast("请输入身份证号");
    } else if (!getApp().globalData.reg.idCard.test(params.idCard)) {
      getApp().globalData.vant.toast("请输入正确的身份证号");
    } else if (!params.community) {
      getApp().globalData.vant.toast("请选择居住小区");
    } else if (!params.cardTopImg) {
      getApp().globalData.vant.toast("请上传身份证人像面");
    } else if (!params.cardImg) {
      getApp().globalData.vant.toast("请上传身份证国徽面");
    } else {
      getApp().globalData.http.communityCert(params).then(res => {
        getApp().globalData.vant.toast({
          type: "success",
          message: "认证成功",
          duration: 1000,
          onClose: () => {
            wx.removeStorageSync("set_community");
            wx.navigateBack();
          },
        });
      }).catch(e => {
        getApp().globalData.vant.toast(e.msg);
      });
    }
  }
})